import axios from 'axios'
import { apiUrl } from '../config/config'
import { message } from 'antd'


// create an axios instance
const service = axios.create({
  baseURL: apiUrl,
  timeout: 1000 * 10,
  withCredentials: true, //表示跨域请求时是否需要使用凭证，默认为false
  headers: {
    'Content-Type': 'application/json',
    'Access-Control-Allow-Origin': '*'
  }
})

// request interceptor
service.interceptors.request.use(
  config => {
    return config
  },

  error => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(

  response => {
    // console.log(response)
    const res = response.data
    if ((res.code / 1) === 305) {
      message.error(res.msg)
      window.location.replace(window.location.origin + '/#/')
      return Promise.reject(new Error(res.msg || 'Error'))
    } else {
      return res
    }
  },
  error => {
    console.log('err', error) // for debug
    return Promise.reject(error)
  }
)

export default service




